﻿using System.Windows.Media;
using System.Windows.Media.Imaging;
using Catel.Articles.Base.Data;
using Catel.Data;
using Catel.MVVM;

namespace Catel.Articles.Base.UI.ViewModels
{
    /// <summary>
    /// ExampleCode view model.
    /// </summary>
    public class ExampleViewModel : ViewModelBase
    {
        #region Constructor & destructor
        /// <summary>
        /// Initializes a new instance of the <see cref="ExampleViewModel"/> class.
        /// </summary>
        /// <param name="example">The example info.</param>
        public ExampleViewModel(ExampleInfo example)
        {
            // Store values
            Example = example;

            // Create commands
            Run = new Command<object, object>(OnRunExecute, OnRunCanExecute);

            // Load image
            if (Example.ImageUri != null)
            {
                // Load bitmapo
                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.UriSource = Example.ImageUri;
                bitmap.EndInit();

                // Set image source
                ImageSource = bitmap;
            }
            else
            {
                // Clear
                ImageSource = null;
            }
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the title of the view model.
        /// </summary>
        /// <value>The title.</value>
        public override string Title { get { return "Example code"; } }

        #region Models
        /// <summary>
        /// Gets or sets the example info.
        /// </summary>
        [Model]
        private ExampleInfo Example
        {
            get { return GetValue<ExampleInfo>(ExampleProperty); }
            set { SetValue(ExampleProperty, value); }
        }

        /// <summary>
        /// Register the Example property so it is known in the class.
        /// </summary>
        public static readonly PropertyData ExampleProperty = RegisterProperty("Example", typeof(ExampleInfo));
        #endregion

        #region View model
        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        [ViewModelToModel("Example")]
        public string Name
        {
            get { return GetValue<string>(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

        /// <summary>
        /// Register the Name property so it is known in the class.
        /// </summary>
        public static readonly PropertyData NameProperty = RegisterProperty("Name", typeof(string));

        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        [ViewModelToModel("Example", "LongDescription")]
        public string Description
        {
            get { return GetValue<string>(DescriptionProperty); }
            set { SetValue(DescriptionProperty, value); }
        }

        /// <summary>
        /// Register the Description property so it is known in the class.
        /// </summary>
        public static readonly PropertyData DescriptionProperty = RegisterProperty("Description", typeof(string));

        /// <summary>
        /// Gets or sets the code.
        /// </summary>
        [ViewModelToModel("Example")]
        public string Code
        {
            get { return GetValue<string>(CodeProperty); }
            set { SetValue(CodeProperty, value); }
        }

        /// <summary>
        /// Register the Code property so it is known in the class.
        /// </summary>
        public static readonly PropertyData CodeProperty = RegisterProperty("Code", typeof(string));

        /// <summary>
        /// Gets or sets the image source.
        /// </summary>
        public ImageSource ImageSource
        {
            get { return GetValue<ImageSource>(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

        /// <summary>
        /// Register the ImageSource property so it is known in the class.
        /// </summary>
        public static readonly PropertyData ImageSourceProperty = RegisterProperty("ImageSource", typeof(ImageSource));
        #endregion
        #endregion

        #region Commands
        /// <summary>
        /// Gets the Run command.
        /// </summary>
        public Command<object, object> Run { get; private set; }

        /// <summary>
        /// Method to check whether the Run command can be executed.
        /// </summary>
        /// <param name="parameter">The parameter of the command.</param>
        private bool OnRunCanExecute(object parameter)
        {
            if (Example == null)
            {
                return false;
            }

            return (Example.ClickHandler != null);
        }

        /// <summary>
        /// Method to invoke when the Run command is executed.
        /// </summary>
        /// <param name="parameter">The parameter of the command.</param>
        private void OnRunExecute(object parameter)
        {
            Example.ClickHandler();
        }
        #endregion

        #region Methods
        #endregion
    }
}
